package com.winchain.chainbill.risk.biz.access.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.risk.base.po.RmAccess;
import com.winchain.chainbill.risk.base.po.RmAccessQuota;
import com.winchain.chainbill.risk.base.service.RmAccService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AccessModCmd implements ICommand<RmAccessQuota>, ICallback<RmAccessQuota, AcctTxRec> {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private RmAccService rmAccService;

	public boolean execute(CommandContext<RmAccessQuota> context) throws Exception {
		RmAccessQuota rmAccessQuota = (RmAccessQuota) context.getSource();
		RmAccess access = this.rmAccService.getAccessByBuyerAndSellerId(rmAccessQuota.getBuyerId(),
				rmAccessQuota.getSellerId());
		if ("C".equals(access.getAccState())) {
			access.setAccState("0");
		} else {
			access.setAccState("C");
		}
		this.rmAccService.saveOrUpdate(access);

		return false;
	}

	public boolean callback(CallbackContext<RmAccessQuota, AcctTxRec> context) throws Exception {
		execute(context);

		RmAccessQuota rmAccessQuota = (RmAccessQuota) context.getSource();
		RmAccess access = this.rmAccService.getAccessByBuyerAndSellerId(rmAccessQuota.getBuyerId(),
				rmAccessQuota.getSellerId());
		access.setArUpperLim(rmAccessQuota.getArUpperLimMod());
		access.setConvertRatio(rmAccessQuota.getConvertRatioMod());
		access.setLoanAvl(rmAccessQuota.getLoanAvlMod());
		access.setOutputAvl(rmAccessQuota.getOutputAvlMod());

		this.rmAccService.saveOrUpdate(access);
		return false;
	}
}
